package listbyorder.access001_100.test5;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/6 14:23
 */
public class Solution2 {

    // 方法二： 动态规划（非常清晰的思路）
    public String longestPalindrome(String s) {
        if (s == null || s.equals("") || s.length() < 2) return s;
        int len = s.length();
        boolean[][] dp = new boolean[len - 1][len - 1];
        int res = 0;
        int left = 0;
        int right = 0;
        for (int i = 1; i <= len; i++) {
            for (int j = 0; j + i - 1 < len; j++) {
                if (i == 1) {
                    dp[j][j] = true;
                } else if (s.charAt(j) == s.charAt(j + i - 1)) {
                    if (i <= 3) {
                        dp[j][j + i - 1] = true;
                    } else {
                        dp[j][j + i - 1] = dp[j + 1][j + i - 2];
                    }
                }
                if (dp[j][j + i - 1] && i > res) {
                    res = i;
                    left = j;
                    right = j + i;
                }
            }
        }
        return s.substring(left, right);
    }
}
